package com.echo.servlet;

import java.util.ArrayList;


public class Miner {
	private int docNum;
	private int termNum;
	float[] avgTF = null;
	float[] maxTF = null;
	float[] minTF = null;
	ArrayList<Float> origTFList;
	ArrayList<Float> normTFList;
	ArrayList<Float> TFRList;
	
	Miner(){
		this.docNum = 0;
		this.termNum = 0;
	}
	
	Miner(int myDocNum, int myTermNum){
		this.docNum = myDocNum;
		this.termNum = myTermNum;
		avgTF = new float[termNum];
		maxTF = new float[termNum];
		minTF = new float[termNum];
		origTFList = new ArrayList<Float>();
	}
	
	public void setDocNum(int myDocNum){
		this.docNum = myDocNum;
	}
	
	public int getDocNum(){
		return this.docNum;
	}
	
	public void setTermNum(int myTermNum){
		this.termNum = myTermNum;
	}
	
	public int getTermNum(){
		return this.termNum;
	}
	
	public void computeNormTF(String s){
		String[] strTF = s.split(" "); // String format of all candidate TFs
		//ArrayList<Float> intTF = new ArrayList<Float>(); // Integer format of all candidate TFs
		for (int i = 0; i < strTF.length; i++){
			origTFList.add(Float.parseFloat(strTF[i]));
		}
		normTFList = new ArrayList<Float>(origTFList);
		
		
		for (int i = 0; i < this.termNum; i++){
			maxTF[i] = avgTF[i] = 0; // initialization
			minTF[i] = Float.MAX_VALUE;
		} 
		for (int j = 0; j < this.termNum; j++){
			float tempSum = 0;
			for (int k = 0; k < this.docNum; k++){
				float tempTF = origTFList.get(j+k*this.termNum);
				tempSum = (float)(tempSum + tempTF);
				if(tempTF > maxTF[j])
					maxTF[j] = tempTF;
				if(tempTF < minTF[j])
					minTF[j] = tempTF;
			}
			avgTF[j] = (float) 1.0*tempSum / this.docNum;
		}
		
		// print out for test only
		for (int i = 0; i < this.termNum; i++){
			System.out.print(avgTF[i] + " ");
		}
		System.out.print('\n');
		
		for (int i = 0; i < this.termNum; i++){
			System.out.print(minTF[i] + " ");
		}
		System.out.print('\n');
		
		for (int i = 0; i < this.termNum; i++){
			System.out.print(maxTF[i] + " ");
		}
		System.out.print('\n');
		
		for (int j = 0; j < this.termNum; j++){
			for (int k = 0; k < this.docNum; k++){
				float tempTF = origTFList.get(j+k*this.termNum);
				if(tempTF > avgTF[j])
					normTFList.set(j+k*this.termNum, maxTF[j]);
				else if(tempTF <= avgTF[j])
					normTFList.set(j+k*this.termNum, minTF[j]);
			}
		}
		
		System.out.println("Normalized term frequency values are: ");
		for(int i = 0; i < normTFList.size(); i++){
			System.out.print(normTFList.get(i) + " ");
		}
		System.out.print('\n');
		
	}
	
	public void computeTFR(){
		TFRList = new ArrayList<Float>(normTFList);
		float[] normTFSum = new float[termNum];
		
		for (int j = 0; j < this.termNum; j++){
			for (int k = 0; k < this.docNum; k++){
				normTFSum[j] += normTFList.get(j+k*this.termNum);
			}
		}
		
		for (int j = 0; j < this.termNum; j++){
			for (int k = 0; k < this.docNum; k++){
				float tempTFR = normTFList.get(j+k*this.termNum) / normTFSum[j];
				TFRList.set(j+k*this.termNum, tempTFR);
			}
		}
		
		System.out.println("\nterm frequency RATEs are: ");
		for(int i = 0; i < TFRList.size(); i++){
			System.out.print(TFRList.get(i) + " ");
		}
		System.out.print('\n');
	}
	
	// test only!
	public static void main(String[] args){
		//String s1 = new String("1.2 2.5 5.0 1.3 2.6 4.0 1.4 2.7 0.0");
		String s2 = new String("5 3 4 1 0 1 1 0");
		Miner miner = new Miner(4,2); 
		miner.computeNormTF(s2);
		miner.computeTFR();
	}
	
}
